//
//  main.c
//  dc-test
//
//  Created by edz on 2020/7/16.
//  Copyright © 2020 edz. All rights reserved.
//

#include <stdio.h>

#if 0
// L(n) = L(n-1) + n
int calc_line_spaces(int n){
    if (n == 0) {
        return 1;
    }
    return calc_line_spaces(n-1) + n;
}

int main(){
    int n = 3;
    int m = calc_line_spaces(n);
    printf("%d\n",m);
    return 0;
}
#endif

#if 0
int calc_zig_spaces(int n){
    if(n == 0)
        return 1;
    return calc_zig_spaces(n-1) + 4 * n - 3;
}

int main(){
    int n = 3;
    int m = calc_zig_spaces(n);
    printf("%d\n",m);
    return 0;
}
#endif


#if 0
#include <stdio.h>
//打印三角形
void draw(unsigned int n){
    int a[n][n];
    for (int i = 0; i < n; i++) {
        for (int j = 0; j <= i; j++) {
            if (j == 0 || i == j) {
                a[i][j] = i+1;
            }
            else{
                a[i][j] = a[i-1][j-1] + a[i-1][j];
            }
        }
    }
    for (int i = 0; i < n; i++) {
        for (int k = n - i - 2; k >= 0; k--) {
            printf("  ");
        }
        for (int j = 0; j <= i; j++) {
            printf("%4d",a[i][j]);
        }
        printf("\n");
    }
}

int main(){
    int n = 5;
    draw(n);
    return 0;
}
#endif


#if 0
#include <stdio.h>
#include <math.h>
// 实现atof函数
double my_atof(char *nptr){
    char* p = nptr;
    double result = 0;
    double xiaoshu = 0;
    int flag = 0;
    int n = 0; // 小数点后数字出现次数
    if (*p == ' ') {        // 跳过前面的空格字符
        p++;
    }
    for (; *p; p++) {
        if (*p == '.') {
            flag = 1;
            p++;
        }
        if (flag == 0) {
            if (*p >= '0' && *p <= '9') {
                result = 10 * result + *p - '0';
            }
            else if ( *p == '.'){
                flag = 1;
                p++;
            }
            else
                break;
        }
        else {
            if (*p >= '0' && *p <= '9') {
                n--;
                xiaoshu += (*p - '0') * pow(10,n);
            }
            else
                break;
        }
    }
    return result + xiaoshu;
}

int main(){
    char* s = "0.123";
    double a = my_atof(s);
    printf("%lf\n",a);
    return 0;
}
#endif


#if 0
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>

typedef struct stack{
    int elem;
    struct stack *next;
} Stack, Queue;

Stack* init_stack(void);
void free_stack(Stack* head);
int push(Stack* head, int elem);
int pop(Stack* head);
int top(Stack* head);
int is_full(Stack* head);
int is_empty(Stack* head);
void enqueue(Queue* queue, int data);
int dequeue(Queue* queue);

Stack* init_stack(void) {
    Stack* head = malloc(sizeof(Stack));
    head->next = NULL;
    return head;
}


void free_stack(Stack* head) {
    Stack* t;
    while(head->next != NULL) {
        t = head->next;
        free(head);
        head = t;
    }
    free(head);
}

/*
 * ===  FUNCTION  =============================================================
 *         Name:  push
 *  Description:  将元素elem压栈
 *       Return:  成功-1；失败-0
 * ============================================================================
 */
int push(Stack* head, int elem) {
    Stack *new_node = malloc(sizeof(Stack));
    if (new_node == NULL) {
        // 分配内存失败
        return 0;
    }
    new_node->elem = elem;
    new_node->next = head->next;
    head->next = new_node;
    return 1;
}


/*
 * ===  FUNCTION  =============================================================
 *         Name:  pop
 *  Description:  弹出栈顶节点, 返回该节点的元素值
 *                注意栈为空时应报错
 * ============================================================================
 */
int pop(Stack* head) {
    if (is_empty(head)) {
        printf("stack is empty\n");
        return INT_MIN;
    }
    Stack* t = head->next;
    head->next = t->next;
    int elem = t->elem;
    free(t);
    return elem;
}


/*
 * ===  FUNCTION  =============================================================
 *         Name:  top
 *  Description:  返回栈顶节点元素的值
 *                注意栈为空时应报错
 * ============================================================================
 */
int top(Stack* head) {
    if (is_empty(head)) {
        printf("stack is empty\n");
        return INT_MIN;
    }
    return head->next->elem;
}


/*
 * ===  FUNCTION  =============================================================
 *         Name:  is_empty
 *  Description:
 *       Return: 1: 为空； 0: 不为空
 * ============================================================================
 */
int is_empty(Stack* head) {
    return (head->next == NULL)?1:0;
}

void enqueue(Queue* queue, int data){
    push(queue, data);
}

int dequeue(Queue* queue){
    Stack* s = init_stack();
    
    while (!is_empty(queue)) {
        push(s, pop(queue));
    }
    
    int result = pop(s);
    
    while (!is_empty(s)) {
        push(queue, pop(s));
    }
    
    return result;
}


int main() {
    Queue* queue = init_stack();
    int a[5] = {1, 2, 3, 4, 5};
    for( int i = 0; i < 5; i++) {
    enqueue(queue, a[i]);
    }
    
//    Stack* s = init_stack();
//    while (!is_empty(queue)) {
//        push(s, pop(queue));
//    }
    
    for (int i = 0; i < 5; i++) {
    int out = dequeue(queue);
    printf("%3d", out);
    }
    printf("\n");
    return 0;
}
#endif
